var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "name",
			name : "名称",
			isSort:true,
		}, {
			colkey : "code",
			name : "编码",
			isSort:true,
		}, {
			colkey : "shorthand",
			name : "助记码",
			isSort:true,
		}, {
			colkey : "specification",
			name : "规格型号",
			isSort:true,
		}, {
			colkey : "proTitle",
			name : "促销活动",
		}, {
			colkey : "unitName",
			name : "单位",
		}, {
			colkey : "className",
			name : "分类",
		}, {
			colkey : "brandName",
			name : "品牌",
		}, {
			colkey : "costPrice",
			name : "成本价",
		}, {
			colkey : "marketPrice",
			name : "市场价",
		}, {
			colkey : "quantity",
			name : "库存",
		}, {
			colkey : "createTime",
			name : "创建时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd");
			}
		}/*, {
			name : "操作",
			renderData : function( rowindex ,data, rowdata, colkeyn) {
				return "测试渲染函数";
			}
		}*/ ],
		jsonUrl : rootPath + '/goods/selectList',
		checkbox : true,
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#editGoods").click("click", function() {
		editGoods();
	});
	$("#deleteGoods").click("click", function() {
		deleteGoods();
	});
	$("#addGift").click("click",function(){
		addGift();
	});
});
function editGoods() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	
	loadPage(rootPath+"/goods/addOrEdit?goodsId="+cbox);
}
function deleteGoods() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/goods/deleteGoods';
		var result = CommnUtil.ajax_req(url, {
			ids : cbox.join(",")
		}, "json");
		if (result) {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
function addGift() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	
	//loadPage(rootPath+"/promotion/edit?id="+cbox);
	pageii = layer.open({
		title : "编辑",
		type : 2,
		maxmin : true,
		resize :true,
		area : [ "60%", "60%" ],
		content : rootPath + '/goods/addGift?id=' + cbox
	});
}
